/*
 * @Author: Kiro AI Assistant
 * @Date: 2025-01-10
 * @Description: 悬浮播放器功能示例
 */

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../viewmodels/music_player_view_model.dart';
import '../models/music_item_model.dart';
import '../services/floating_player_service.dart';
import '../pages/music_player_page.dart';

/// 悬浮播放器功能示例页面
class FloatingPlayerExample extends StatefulWidget {
  const FloatingPlayerExample({super.key});

  @override
  State<FloatingPlayerExample> createState() => _FloatingPlayerExampleState();
}

class _FloatingPlayerExampleState extends State<FloatingPlayerExample>
    with FloatingPlayerPageMixin {
  @override
  void initState() {
    super.initState();
    initFloatingPlayer();
  }

  @override
  void dispose() {
    disposeFloatingPlayer();
    super.dispose();
  }

  /// 创建测试音乐列表
  List<MusicItemModel> _createTestPlaylist() {
    return [
      const MusicItemModel(
        id: 'test_001',
        title: '夜曲',
        artist: '周杰伦',
        album: '十一月的萧邦',
        albumArt:
            'https://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg',
        audioUrl: 'https://music.163.com/song/media/outer/url?id=553310243.mp3',
        duration: Duration(minutes: 4, seconds: 34),
        sourceType: 'test',
      ),
      const MusicItemModel(
        id: 'test_002',
        title: '青花瓷',
        artist: '周杰伦',
        album: '我很忙',
        albumArt:
            'https://y.gtimg.cn/music/photo_new/T002R300x300M000002Neh8l0uciQZ.jpg',
        audioUrl: 'https://music.163.com/song/media/outer/url?id=425570952.mp3',
        duration: Duration(minutes: 3, seconds: 58),
        sourceType: 'test',
      ),
      const MusicItemModel(
        id: 'test_003',
        title: '稻香',
        artist: '周杰伦',
        album: '魔杰座',
        albumArt:
            'https://y.gtimg.cn/music/photo_new/T002R300x300M000002Neh8l0uciQZ.jpg',
        audioUrl: 'https://music.163.com/song/media/outer/url?id=425570953.mp3',
        duration: Duration(minutes: 3, seconds: 43),
        sourceType: 'test',
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('悬浮播放器示例'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: Consumer<MusicPlayerViewModel>(
        builder: (context, musicPlayerViewModel, child) {
          return Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                // 当前播放信息
                Card(
                  child: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Text(
                          '当前播放信息',
                          style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        const SizedBox(height: 8),
                        Text('标题: ${musicPlayerViewModel.currentTrackTitle}'),
                        Text('艺术家: ${musicPlayerViewModel.currentTrackArtist}'),
                        Text('专辑: ${musicPlayerViewModel.currentTrackAlbum}'),
                        const SizedBox(height: 8),
                        ValueListenableBuilder<bool>(
                          valueListenable: musicPlayerViewModel.isPlaying,
                          builder: (context, isPlaying, child) {
                            return Text(
                              '播放状态: ${isPlaying ? "播放中" : "已暂停"}',
                              style: TextStyle(
                                color: isPlaying ? Colors.green : Colors.red,
                                fontWeight: FontWeight.bold,
                              ),
                            );
                          },
                        ),
                      ],
                    ),
                  ),
                ),

                const SizedBox(height: 16),

                // 控制按钮
                const Text(
                  '播放控制',
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 8),

                ElevatedButton(
                  onPressed: () {
                    final playlist = _createTestPlaylist();
                    musicPlayerViewModel.setPlaylist(playlist);
                    musicPlayerViewModel.play(playlist.first);
                  },
                  child: const Text('开始播放测试音乐'),
                ),

                const SizedBox(height: 8),

                ElevatedButton(
                  onPressed: musicPlayerViewModel.hasCurrentTrack
                      ? () => musicPlayerViewModel.togglePlayPause()
                      : null,
                  child: ValueListenableBuilder<bool>(
                    valueListenable: musicPlayerViewModel.isPlaying,
                    builder: (context, isPlaying, child) {
                      return Text(isPlaying ? '暂停播放' : '继续播放');
                    },
                  ),
                ),

                const SizedBox(height: 8),

                ElevatedButton(
                  onPressed: musicPlayerViewModel.hasCurrentTrack
                      ? () => musicPlayerViewModel.stop()
                      : null,
                  child: const Text('停止播放'),
                ),

                const SizedBox(height: 16),

                // 悬浮播放器控制
                const Text(
                  '悬浮播放器控制',
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 8),

                ElevatedButton(
                  onPressed: () => showFloatingPlayer(),
                  child: const Text('显示悬浮播放器'),
                ),

                const SizedBox(height: 8),

                ElevatedButton(
                  onPressed: () => hideFloatingPlayer(),
                  child: const Text('隐藏悬浮播放器'),
                ),

                const SizedBox(height: 8),

                ElevatedButton(
                  onPressed: () => toggleFloatingPlayer(),
                  child: const Text('切换悬浮播放器显示状态'),
                ),

                const SizedBox(height: 16),

                // 页面导航
                const Text(
                  '页面导航测试',
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 8),

                ElevatedButton(
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => const MusicPlayerPage(),
                      ),
                    );
                  },
                  child: const Text('进入播放页面（悬浮播放器应隐藏）'),
                ),

                const SizedBox(height: 8),

                ElevatedButton(
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => const _TestPage(),
                      ),
                    );
                  },
                  child: const Text('进入其他页面（悬浮播放器应显示）'),
                ),

                const Spacer(),

                // 状态信息
                Text(
                  '悬浮播放器状态: ${FloatingPlayerService.isShowing ? "显示中" : "已隐藏"}',
                  style: TextStyle(
                    color: FloatingPlayerService.isShowing
                        ? Colors.green
                        : Colors.grey,
                    fontWeight: FontWeight.bold,
                  ),
                  textAlign: TextAlign.center,
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}

/// 测试页面
class _TestPage extends StatefulWidget {
  const _TestPage();

  @override
  State<_TestPage> createState() => _TestPageState();
}

class _TestPageState extends State<_TestPage> with FloatingPlayerPageMixin {
  @override
  void initState() {
    super.initState();
    initFloatingPlayer();
  }

  @override
  void dispose() {
    disposeFloatingPlayer();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('测试页面'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: const Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              '这是一个测试页面',
              style: TextStyle(fontSize: 24),
            ),
            SizedBox(height: 16),
            Text(
              '在这个页面中，悬浮播放器应该显示',
              style: TextStyle(fontSize: 16),
            ),
            SizedBox(height: 16),
            Text(
              '点击悬浮播放器可以返回播放页面',
              style: TextStyle(fontSize: 16, color: Colors.grey),
            ),
          ],
        ),
      ),
    );
  }
}
